package br3akr.DeadlyDinnerParty.Server.World;

import java.util.HashSet;
import java.util.Set;

import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;

public class Chest {
	String name;
	String description;
	Set<Item> contents;
	boolean locked;
	
	public Chest(String name, String description) {
		this.name = name;
		this.description = description;
		contents = new HashSet<Item>();
	}
	
	public String getName() {
		return name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void addItem(Item item) {
		if (item != null) {
			contents.add(item);
		}
	}
	
	public Item removeItem(Item item) {
		Item retval = null;
		if (item != null) {
			for (Item i : contents) {
				if (item.equals(i)) {
					retval = i;
					contents.remove(item);
					break;
				}
			}
		}
		
		return retval;
	}
	
	public Item getItem(String name) throws DeadlyDinnerPartyException {
		Item retval = null;
		
		for (Item i : contents) {
			if (i.getName().toUpperCase().equals(name.toUpperCase())) {
				retval = i;
				break;
			} else if (i.getName().toUpperCase().startsWith(name.toUpperCase())) {
				if (retval == null) {
					retval = i;
				} else {
					throw new DeadlyDinnerPartyException("Which item?");
				}
			}
		}
		return retval;
	}
	
	public boolean isLocked() {
		return locked;
	}
	
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	
	public Set<Item> getContents() {
		return contents;
	}
}
